#include "dialogFilter2.h"

PosiTable::PosiTable(QWidget* parent){
    this->setParent(parent);
    this->installEventFilter(parent);
    this->viewport()->installEventFilter(this);

}

bool PosiTable::eventFilter(QObject *watched, QEvent *event){
    if(event->type() == QEvent::MouseButtonRelease){
        qDebug("table鼠标释放事件 - 发送信号");
        emit sigMouseRelease();
    }
    return QWidget::eventFilter(watched, event);
}


DialogFilter2::DialogFilter2(QWidget * parent)
{

#ifdef Q_OS_ANDROID
    this->setParent(parent);
#else
    //this->setParent(parent);
#endif

    this->setWindowTitle("筛选");

#ifdef Q_OS_ANDROID
    this->setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
#else
    this->setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint ^ Qt::WindowStaysOnTopHint);
    this->setModal(false);
#endif

#ifdef Q_OS_ANDROID
    setWindowFlags(Qt:: Dialog | Qt:: FramelessWindowHint);
    this->setStyleSheet("QDialog{background-color: white; padding:20px; border: none; border: 10px solid rgb(200,219,243);}");
#else
    this->setStyleSheet("background-color: white;");
#endif
    _layout = new QVBoxLayout(this);
    _table_posi = new PosiTable(this);
    _layout_posi = new QHBoxLayout(this);
    _layout_combo = new QHBoxLayout(this);
    _layout_button = new QHBoxLayout(this);

    _button_yes = new QPushButton("确定", this);
    _button_no = new QPushButton("取消", this);

    _layout->addLayout(_layout_posi);
    _layout->addLayout(_layout_combo);
    _layout->addLayout(_layout_button);

    _lcBox_name = new LLComboBox2(this);
    _lcBox_type = new LLComboBox2(this);

    _lcBox_name->setMinimumWidth(180);
    _lcBox_type->setMinimumWidth(180);

    _layout->setSpacing(15);
    _button_no->setFixedSize(QSize(130, 40));
    _button_no->setStyleSheet("background: #FFFFFF;\nborder-radius: 8px;\nborder: 1px solid #888888;");
    _button_yes->setFixedSize(QSize(130, 40));
    _button_yes->setStyleSheet("background-color: qlineargradient(x1: 0, y0: 0, x2: 0, y2: 1, stop: 0 #8C97ED, stop: 1 #0018D1);\nborder-radius: 8px; \ncolor: white;");


    _layout_posi->addWidget(_table_posi);
    _table_posi->setEditTriggers(QAbstractItemView::NoEditTriggers);
    _table_posi->setRowCount(8);
    _table_posi->setColumnCount(12);

    _table_posi->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    _table_posi->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    _table_posi->horizontalHeader()->setStyleSheet("QHeaderView::section{background:#EBF3FF; border: solid;}");
    _table_posi->horizontalHeader()->setFixedHeight(30);
    _table_posi->verticalHeader()->setStyleSheet("QHeaderView::section{background:#EBF3FF; border: solid;}");
    _table_posi->verticalHeader()->setFixedWidth(30);
    _table_posi->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    _table_posi->setCornerWidget(new QWidget(_table_posi));
    _table_posi->cornerWidget()->setStyleSheet("background:#EBF3FF; border: solid;");
    _table_posi->setVerticalHeaderLabels(QStringList({"A", "B", "C", "D", "E", "F", "G", "H"}));


    _table_posi->setFixedHeight(360);
    _table_posi->setFixedWidth(550);


    _layout_combo->addSpacerItem(new QSpacerItem(40, 40, QSizePolicy::Expanding));
    _layout_combo->addWidget(new QLabel("样本名称",this));
    _layout_combo->addWidget(_lcBox_name);
    _layout_combo->addWidget(new QLabel("样本类型",this));
    _layout_combo->addWidget(_lcBox_type);
    _layout_combo->addSpacerItem(new QSpacerItem(40, 40, QSizePolicy::Expanding));

    _layout_button->addSpacerItem(new QSpacerItem(40, 40, QSizePolicy::Expanding));
    _layout_button->addWidget(_button_no);
    _layout_button->addWidget(_button_yes);
    _layout_button->addSpacerItem(new QSpacerItem(40, 40, QSizePolicy::Expanding));

    this->setMouseTracking(true);

    connect(_button_yes, SIGNAL(clicked()), this, SLOT(on_yesClicked()));
    connect(_button_no, SIGNAL(clicked()), this, SLOT(on_noClicked()));
    connect(_table_posi, SIGNAL(sigMouseRelease()), this, SLOT(on_posiChanged()));


    this->installEventFilter(this);


}

void DialogFilter2::showEvent(QShowEvent *){


    if(!pState) return;

    posiList.clear();
    foreach(QString posi, pState->posiList_filter){
        posiList.insert(posi);
    }


    // 孔位
    foreach(QString posi, posiList){
        int r = posi[0].toLatin1() - 'A';
        int c = posi.mid(1).toInt() - 1;
        if(! _table_posi->item(r, c)){
            return;
        }
        _table_posi->item(r,c)->setBackground(QColor("#A3AEFF"));
    }

#ifdef Q_OS_ANDROID
    if(this->parent() != nullptr)
        this->setGeometry(((QWidget*)parent())->width()/2-300, ((QWidget*)parent())->height()/2-300, 600, 580);
#else
    this->layout()->setMargin(30);
    this->setFixedSize(600, 600);
#endif

}

void DialogFilter2::init(PCRState* state){

    if(!state) return;
    pState = state;
    for(int r = 0; r < 8; r ++){
        for(int c = 0; c < 12; c ++){
            QString posi = ('A'+r)+QString::number(c+1);
            QString text = pState->posiList.contains(posi) ? posi : "";
            _table_posi->setItem(r, c, new QTableWidgetItem(text));
            _table_posi->item(r,c)->setBackground(QColor("#FFFFFF"));
        }
    }

    // 样本名
    foreach(QString name, pState->nameList){
        _lcBox_name->addDataItem(name);
    }
    // 类型
    foreach(QString type, pState->typeList){
        _lcBox_type->addDataItem(type);
    }


}


void DialogFilter2::on_yesClicked(){
    QStringList selectList;

    pState->posiList_filter.clear();
    foreach(QString posi, posiList){
        pState->posiList_filter.append(posi);
    }
    pState->nameList_filter.clear();
    selectList = this->_lcBox_name->selectedDataText();
    foreach(QString selectItem, selectList){
        pState->nameList_filter.append(selectItem);
    }
    pState->typeList_filter.clear();
    selectList = this->_lcBox_type->selectedDataText();
    foreach(QString selectItem, selectList){
        pState->typeList_filter.append(selectItem);
    }
    this->close();
    pState->emitFilterChanged();
}

void DialogFilter2::on_noClicked(){
    this->close();
}

void DialogFilter2::on_posiChanged(){

    QList<QTableWidgetSelectionRange> rangeList = _table_posi->selectedRanges();

    QVector<QString>        selectedList;
    foreach(QTableWidgetSelectionRange range, rangeList){
        for(int r = range.topRow(); r <= range.bottomRow(); r ++){
            for(int c = range.leftColumn(); c <= range.rightColumn(); c ++){
                if(_table_posi->item(r, c)->text() != ""){
                    selectedList.append(_table_posi->item(r, c)->text());
                }
            }
        }

        _table_posi->setRangeSelected(range, false);
    }


    // 选区内全是黑色：全置白
    // 选区内存在白色：全置黑
    bool hasWhite = false;
    foreach(QString posi, selectedList){
        if(! posiList.contains(posi)){
            hasWhite = true;
            break;
        }
    }


    if(hasWhite == true){
        foreach(QString posi, selectedList){
            posiList.insert(posi);
        }
    }else{
        foreach(QString posi, selectedList){
            posiList.remove(posi);
        }
    }

    // 孔位
    foreach(QString posi, pState->posiList){

        int r = posi[0].toLatin1() - 'A';
        int c = posi.mid(1).toInt() - 1;
        if(! _table_posi->item(r, c)){
            qDebug() << "DialogFilter2-showEventError" << r << c;
            return;
        }
        if(posiList.contains(posi)){
            _table_posi->item(r,c)->setBackground(QColor("#A3AEFF"));
        }else{
            _table_posi->item(r,c)->setBackground(QColor("#FFFFFF"));
        }
    }

}


